package logic

import (
	"context"
	"fmt"
	"week2/user_srv/model/redis"

	"week2/user_srv/internal/svc"
	"week2/user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsCodeLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsCodeLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsCodeLogLogic {
	return &SmsCodeLogLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsCodeLogLogic) SmsCodeLog(in *user.SmsCodeLogRequest) (*user.SmsCodeResponse, error) {
	// todo: 短信验证码登录

	//获取缓存中的短信验证
	code := redis.GetSmsCode(in.Mobile, "login")

	fmt.Println("缓存的数据对比", code+"=="+in.Code)

	if code != in.Code {
		logx.Error("验证码不匹配")
		return nil, nil
	}
	//验证码匹配销毁验证码
	redis.DelSmsCode(in.Mobile, "login")
	return &user.SmsCodeResponse{
		Code: code,
	}, nil

}
